import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    redirect: '/home',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "about" */ '../views/Home/Home.vue'),
        meta: {
          title: "首页",
          icon: "wap-home-o"
        }
      },
      {
        path: 'classfy',
        name: 'Classfy',
        component: () => import(/* webpackChunkName: "about" */ '../views/Home/Classfy.vue'),
        meta: {
          title: "分类",
          icon: "wap-home-o"
        }
      },
      {
        path: 'message',
        name: 'Message',
        component: () => import(/* webpackChunkName: "about" */ '../views/Home/Message.vue'),
        meta: {
          title: "信息",
          icon: "wap-home-o"
        }
      },
      {
        path: 'shopcar',
        name: 'Shopcar',
        component: () => import(/* webpackChunkName: "about" */ '../views/Home/Shopcar.vue'),
        meta: {
          title: "购物车",
          icon: "wap-home-o"
        }
      },
      {
        path: 'my',
        name: 'My',
        component: () => import(/* webpackChunkName: "about" */ '../views/Home/My.vue'),
        meta: {
          title: "我的",
          icon: "wap-home-o"
        }
      }
    ]
  },
  {
    path: '/address',
    name: 'Address',
    component: () => import(/* webpackChunkName: "about" */ '../views/Address.vue')
  },
  {
    path: '/Detail',
    name: 'Detail',
    component: () => import(/* webpackChunkName: "about" */ '../views/Detail.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/notfound',
    name: 'Notfound',
    component: () => import(/* webpackChunkName: "about" */ '../views/Notfound.vue')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import(/* webpackChunkName: "about" */ '../views/Search.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

router.beforeEach((to, from, next) => {
  console.log(to);
  document.write = to.meta.title;
  if (to.name == 'My' || to.name == 'Shopcar') {
    console.log('要到我的页面了');
    // 从本地获取一下登录状态
    const isLogin = localStorage.getItem('isLogin')
    // 判断是否已经登录了
    console.log(isLogin);
    if (isLogin == 'true') {
      // 登录了不用管
      console.log('您已经登录了');
    } else {
      console.log('您还未登录');
      // 未登录，跳转到登录页面
      next('/login')
    }
  }

  next()
})